package com.webside.points.controller;


import java.io.File;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSON;
import com.webside.points.model.AdvertisingEntity;
import com.webside.points.service.AdvertisingService;
import com.webside.points.wechat.UploadUtil;

@Controller
@Scope("prototype")
@RequestMapping("/advertising/")
public class AdvertisingController {

	
	@Autowired
	AdvertisingService advertisingService;
	
	@RequestMapping("findAll")
	public String findAll(Model model){
		
		List<AdvertisingEntity> list = advertisingService.findAll();
		
		model.addAttribute("advertisingList", list);

		return "serve/advertising";
	}
    @RequestMapping("insert")
    public String insert(AdvertisingEntity advertisingEntity, @RequestParam("file") CommonsMultipartFile file,HttpServletRequest request){
    	/*if(advertisingEntity.getLink().equals("")){
    		advertisingEntity.setLink("#");
    	}*/
    	//是否显示
    	if(advertisingEntity.getStatus() != null){
    		AdvertisingEntity advertising = new AdvertisingEntity();
    		advertising.setLocation(advertisingEntity.getLocation());
    		advertising.setStatus("0");
    		advertisingService.updateStatus(advertising);
    	}else{
    		advertisingEntity.setStatus("0");
    	}
    	//上传图片
    	String image = UploadUtil.uploadImage(file, request);
    	if(image != null){
    		 advertisingEntity.setImage(image);
    	}
    	advertisingEntity.setCreateTime(new Date(System.currentTimeMillis()));
    	advertisingService.insert(advertisingEntity);
        return "redirect:findAll";
    }
    
    @RequestMapping("delete")
    public String delete(int id){
    	AdvertisingEntity advertisingEntity = advertisingService.findById(id);
    	
    	System.out.println(advertisingEntity);
    	int result = advertisingService.delete(id);
    	if(result==1){
    		String path=advertisingEntity.getImage();
    		boolean bool = UploadUtil.removeImage(path);
    		System.out.println("bool:"+bool);
    		
    		File file = new File(path);
    		
    		System.out.println(file.delete());
    	}
    	
    	
    	return "redirect:findAll";
    }
    
    @RequestMapping("findByLocation")
    public void findByLocation(String location,HttpServletResponse response){
    	
    	System.out.println("进入广告位置查询：>>>"+location);
    	AdvertisingEntity advertisingEntity = advertisingService.findByLocation(location);
    	
    	String json = JSON.toJSONString(advertisingEntity);
		try {
			response.getWriter().write(json);
			response.flushBuffer();
		} catch (Exception e) {
			System.out.println("JsonError："+e);
		}
		
    }
    
    @RequestMapping("findById")
    public String findById(int id,Model model){
    	System.out.println("进入广告查询：>>>"+id);
    	AdvertisingEntity  advertisingEntity = advertisingService.findById(id);
    	model.addAttribute("advertising", advertisingEntity);
    	
    	return "serve/advertisingForm";
    }
    
    @RequestMapping("update")
    public String update(AdvertisingEntity advertisingEntity, @RequestParam("file") CommonsMultipartFile file,HttpServletRequest request){
    	System.out.println("进入广告修改：>>>"+advertisingEntity+"  "+file.isEmpty());
    	AdvertisingEntity advertising = advertisingService.findById(advertisingEntity.getId());
    	//检查是否需要上次图片
    	if(!advertisingEntity.getImage().equals(advertising.getImage())){
    		String image = UploadUtil.uploadImage(file, request);
        	if(image != null){
        		 advertisingEntity.setImage(image);
        	}
    	}
    	advertisingService.update(advertisingEntity);
    	
    	
        return "redirect:findAll";
    }
    
    @RequestMapping("findAllAdvertising")
    public void findAllAdvertising(HttpServletResponse response){
    	List<AdvertisingEntity> list = advertisingService.findAll();
    	String json = JSON.toJSONString(list);
		try {
			response.getWriter().write(json);
			response.flushBuffer();
		} catch (Exception e) {
			System.out.println("JsonError："+e);
		}
    }
    
    @RequestMapping("updateStatus")
    public void updateStatus(AdvertisingEntity advertisingEntity,HttpServletResponse response){
    	System.out.println("进入广告修改状态：>>>"+advertisingEntity);
    	int result = advertisingService.updateStatus(advertisingEntity);
    	String json = JSON.toJSONString(result);
		try {
			response.getWriter().write(json);
			response.flushBuffer();
		} catch (Exception e) {
			System.out.println("JsonError："+e);
		}
    }


}
